package org.xxx.epms.sequrity.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.xxx.epms.models.authority.model.AdminPermission;
import org.xxx.epms.models.epms.model.Admin;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class AdminDetails implements UserDetails {

    private Admin admin;

    private List<AdminPermission> adminResourcesList;

    public AdminDetails(Admin admin, List<AdminPermission> adminResourcesList) {
        this.admin = admin;
        this.adminResourcesList = adminResourcesList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return adminResourcesList.stream()
                .map(permission ->new SimpleGrantedAuthority("[Admin-Permission]" + permission.getAdminPermissionName() + "(" +  permission.getAdminPermissionId() + ")" ))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return admin.getAdminPassword();
    }

    @Override
    public String getUsername() {
        return admin.getAdminCode();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
